package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.UserLoginService;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;


@Service
public class UserLoginServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements UserLoginService {
    /**
     * 登录功能
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(AdUserDto dto) {
        //1.参数校验
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名或密码不能为空");
        }

        LambdaQueryWrapper<AdUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdUser::getName, dto.getName());

        List<AdUser> list = list(lambdaQueryWrapper);
        if (list != null && list.size() == 1) {
            AdUser adUser = list.get(0);
            String pwd=DigestUtils.md5DigestAsHex((dto.getPassword()+adUser.getSalt()).getBytes());
            if (StringUtils.equals(pwd,adUser.getPassword())){
                //登陆成功
                Map<String, Object> map = Maps.newHashMap();
                adUser.setPassword("");
                adUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));
                map.put("user", adUser);
                return ResponseResult.okResult(map);
            }else {
                //密码错误
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
    }

    /**
     * 注册功能
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult register(AdUserDto dto) {
        //1.参数校验
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名或密码不能为空");
        }
        LambdaQueryWrapper<AdUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdUser::getName, dto.getName());
        List<AdUser> list = list(lambdaQueryWrapper);
        if (list != null && list.size() >= 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "用户已存在");
        }
        AdUser adUser=new AdUser();
        adUser.setName(dto.getName());
        adUser.setSalt(BCrypt.gensalt());
        String pwd=DigestUtils.md5DigestAsHex((dto.getPassword()+adUser.getSalt()).getBytes());
        adUser.setPassword(pwd);
        save(adUser);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }
}
